<!-- src/components/AvatarSelect.vue -->
<template>
  <div>
    <!-- 搜索过滤 -->
    <el-card class="mb-4">
      <el-form :model="queryParams" :inline="true">
        <el-form-item label="性别" style="width: 180px">
          <el-select v-model="queryParams.gender" placeholder="选择性别" clearable>
            <el-option label="男性" value="male" />
            <el-option label="女性" value="female" />
          </el-select>
        </el-form-item>
        <el-form-item label="类型" style="width: 180px">
          <el-select v-model="queryParams.premium" placeholder="选择类型" clearable>
            <el-option label="基础版" :value="false" />
            <el-option label="高级版" :value="true" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>查询
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 虚拟人物列表 -->
    <div class="avatar-grid">
      <div
          v-for="avatar in avatars"
          :key="avatar.avatarId"
          class="avatar-item"
          :class="{ active: selectedAvatar?.avatarId === avatar.avatarId }"
      >
        <div class="avatar-image" @click="handleSelect(avatar)">
          <el-image
              :src="avatar.previewImageUrl"
              fit="cover"
              loading="lazy"
          >
            <template #error>
              <div class="image-placeholder">
                <el-icon><Picture /></el-icon>
              </div>
            </template>
          </el-image>
          <div class="avatar-tags">
            <el-tag size="small" :type="avatar.gender === 'male' ? 'info' : 'danger'">
              {{ avatar.gender === 'male' ? '男性' : '女性' }}
            </el-tag>
            <el-tag size="small" type="warning" v-if="avatar.premium">
              高级版
            </el-tag>
          </div>
        </div>
        <div class="avatar-info">
          <h3 class="avatar-name">{{ avatar.avatarName }}</h3>
          <div class="avatar-actions" style="text-align: center">
            <el-button
                type="primary"
                link
                size="small"
                @click="handlePreview(avatar)"
            >
              <el-icon><VideoPlay /></el-icon>预览
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="dialog-footer">
      <el-button
          type="primary"
          @click="confirmSelect"
          :disabled="!selectedAvatar"
      >确定</el-button>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :total="avatarStore.total"
          :page-sizes="[12, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <!-- 预览弹窗 -->
    <el-dialog
        v-model="previewDialog.visible"
        title="虚拟人物预览"
        width="630"
        :close-on-click-modal="false"
    >
      <video
          width="600"
          v-if="previewDialog.videoUrl"
          :src="previewDialog.videoUrl"
          controls
          class="preview-video"
      ></video>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {Search, Refresh, Picture, VideoPlay} from '@element-plus/icons-vue'
import { useAvatarStore } from '@/stores/video/avatar'
import { storeToRefs } from 'pinia'
import {ElMessage} from "element-plus";

const emit = defineEmits(['update:modelValue', 'select'])

const avatarStore = useAvatarStore()
const { avatars } = storeToRefs(avatarStore)

// 查询参数
const queryParams = reactive({
  gender: '',
  premium: null,
  pageSize: 12,
})

// 预览弹窗
const previewDialog = reactive({
  visible: false,
  videoUrl: ''
})

// 选中的虚拟人物
const selectedAvatar = ref(null)

// 查询
const handleQuery = () => {
  avatarStore.fetchAvatars(queryParams)
}

// 重置
const resetQuery = () => {
  queryParams.gender = ''
  queryParams.premium = null
  handleQuery()
}

// 选择虚拟人物
const handleSelect = (avatar) => {
  selectedAvatar.value = avatar
}

// 分页方法
const handleSizeChange = (val) => {
  queryParams.pageSize = val
  handleQuery()
}

const handleCurrentChange = (val) => {
  queryParams.pageNum = val
  handleQuery()
}

// 预览
const handlePreview = (avatar) => {
  if (!avatar.previewVideoUrl) {
    ElMessage.warning('暂无预览视频')
    return
  }
  previewDialog.videoUrl = avatar.previewVideoUrl
  previewDialog.visible = true
}

// 确认选择
const confirmSelect = () => {
  if (selectedAvatar.value) {
    emit('select', selectedAvatar.value)
    emit('update:modelValue', false)  // 关闭弹窗
  }
}

// 初始化
onMounted(() => {
  handleQuery()
})
</script>

<style scoped>
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.avatar-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
  padding: 20px 0;
  max-height: 500px;
  overflow-y: auto;
}

.avatar-item {
  border: 2px solid transparent;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s;
}

.avatar-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.avatar-item.active {
  border-color: var(--el-color-primary);
}

.avatar-image {
  position: relative;
  height: 200px;
}

.avatar-image .el-image {
  width: 100%;
  height: 100%;
}

.image-placeholder {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
}

.avatar-tags {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 5px;
}

.avatar-info {
  padding: 14px;
  background: #f5f7fa;
}

.avatar-name {
  margin: 0;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mb-4 {
  margin-bottom: 16px;
}

.dialog-footer {
  text-align: right;
  margin-top: 20px;
}
</style>